<?php

namespace app\common\service\logistics;

use app\common\library\ApiException;
use app\common\model\ExpressComment;
use app\common\model\ExpressOrder;
use app\common\model\ExpressUser;
use think\Db;
use think\Exception;
use think\Request;

/**
 * 评论
 */
class Comment
{

    /**
     * 添加评论
     * @param $auth
     * @param Request $request
     * @return true
     * @throws ApiException
     */
    public function send($auth,Request $request)
    {
        $request->filter(['strip_tags']);
        $content    = $request->param("content","");
        $score      = $request->param("score");
        $express_id = $request->param("express_id");
        $order_id   = $request->param("order_id");
        if(!$content || !$score || !$order_id){
            throw new ApiException("请填写评论内容或评分");
        }
        if($score < 1 || $score > 5){
            throw new ApiException("请输入1~5星评论");
        }
        Db::startTrans();
        try {
            $express_info = ExpressUser::where("id",$express_id)->find();
            if(!$express_info){
                throw new Exception("快递员不存在");
            }

            #是否评论了该订单
            $order = ExpressOrder::where("id",$order_id)->find();
            if(!$order){
                throw new Exception("订单不存在");
            }
            $is_comment = ExpressComment::where("order_id",$order["id"])->count();
            if($is_comment){
                throw new Exception("该订单已经评论");
            }
            #end

            $res = ExpressComment::create([
                "user_id"   => $auth->id,
                "express_id"=> $express_id,
                "content"   => $content,
                "score"     => $score,
                "order_id"  => $order_id
            ]);
            if(!$res){
                throw new Exception("添加失败");
            }
            //综合评分
            $avg_score = ExpressComment::where("express_id",$express_id)->avg("score");
            $express_info->save([
                "comment_score"     => $avg_score
            ]);
            Db::commit();
            return true;
        }catch (Exception $e){
            Db::rollback();
            throw new ApiException($e->getMessage());
        }
    }

    public function com_list(Request $request)
    {
        $express_id = $request->param("express_id");
        $express_info = ExpressUser::where("id",$express_id)->find();
        if(!$express_info){
            throw new Exception("快递员不存在");
        }
        $list = ExpressComment::alias("e")
            ->join("user u","u.id=e.user_id")
            ->where("express_id",$express_id)
            ->field("e.*,u.avatar,u.nickname")
            ->order("id desc")
            ->paginate($request->param("limit",15));
        $currentTime = time();
        foreach($list as $l){
            $createTime = $l->createtime; // 获取创建时间的时间戳
            $timeDiff = $currentTime - $createTime; // 计算时间差（单位：秒）
            $pay_str = "";
            $days = floor($timeDiff / (60 * 60 * 24)); // 计算天数
            if($days > 0){
                $pay_str = sprintf("%s天",$days);
            }
            $hours = floor(($timeDiff % (60 * 60 * 24)) / (60 * 60)); // 计算小时数
            if($hours > 0){
                $pay_str .= sprintf("%s时",$hours);
            }
            $minutes = floor(($timeDiff % (60 * 60)) / 60); // 计算分钟数
            if($minutes > 0){
                $pay_str .= sprintf("%s分",$minutes);
            }
            $seconds = $timeDiff % 60; // 计算秒数
            if($seconds > 0){
                $pay_str .= sprintf("%s秒",$seconds);
            }
            $l->comment_time =  $pay_str;

        }

        return $list;
    }
}